#!/usr/bin/env Rscript

### ####################
### Package Dependencies
### ####################

library("plyr")
library("foreach")
library("emIRT")
library("pscl")
library("ggplot2")
library("grid")
library("scales")

theme_set(theme_bw())

## #########
## Set Paths
## #########

p2root <- "../"
p2figs <- paste(p2root, "doc/figs/", sep = "")
p2times <- paste(p2root, "data/CSV/times/", sep = "")

vAllFiles <- dir(p2times, recursive = TRUE)

dfAll <- foreach(afile = vAllFiles,
                 .combine = rbind
                 ) %do% {
                     df <- read.csv(file = paste(p2times, afile, sep = ""))
                     return(df)
                 }

## ##########################################
## Runtime by Chamber by Session by Estimator
## ##########################################

dfAll$clab <- ifelse(dfAll$cham == "hou", "House", "Senate")

dfLabels <- data.frame(label = c("5s", "1m", "1h"),
                       x = c(101, 101, 101),
                       y = c(5, 60, 60 * 60)
                       )

dfAll2 <- ddply(dfAll,
                c("sess", "type", "clab"),
                .fun = function(x) data.frame(time = median(x$time))
                )


dfAll2$type2 <- dfAll2$type

dfLabs <- subset(dfAll2, sess == 102 & clab == "Senate")

dfLabs$type3 <- NA
dfLabs$type3[dfLabs$type2 == "fastest"] <- "EM (high precision)"
dfLabs$type3[dfLabs$type2 == "fastestMP"] <- "EM (parallel)"
dfLabs$type3[dfLabs$type2 == "fastestLOW"] <- "EM"
dfLabs$type3[dfLabs$type2 == "ideal"] <- "IDEAL"
dfLabs$type3[dfLabs$type2 == "oc"] <- "OC"
dfLabs$type3[dfLabs$type2 == "wnominate"] <- "W-NOMINATE"

dfLabs

dfLabs <- data.frame(clab = "House",
                     x = c(101, 101, 101, 101, 101) - .15,
                     xs = c(101, 101, 101, 101.5, 101.4),
                     xe = c(101, 101, 101, 102.02, 101.9),
                     y = c(60 * .7, 60 * .012, 60 * 60 * 3, 60 * 20, 60 * 2.5),
                     ys = c(60 * .7, 06 * .7, 60 * 60 * 3, 60 * 15, 60 * 2.5),
                     ye = c(0, 0, 0, 60 * 5, 60 * 2.5),
                     lab = c("EM (high precision)", "EM", "IDEAL", "OC", "W-NOMINATE")
                     )


dfAll2a <- subset(dfAll2)
thesetypes <- factor(dfAll2a$type2)

cols1 <- scales::hue_pal(l = 55)(length(unique(thesetypes)))
shapes1 <- shape_pal(solid = TRUE)(length(unique(thesetypes)))

cols1[c(5, 6)] <- cols1[c(6, 5)]

shapes1 <- c(1, 2, 5, 16, 7, 8)

shapes1 <- c(1, 2, 5, 6, 7, 8, 9)

vNames <- c("EM with Bootstrap",
            "EM (high-precision)",
            "EM",
            "EM (parallel high-precision)",
            "IDEAL",
            "OC",
            "W-NOMINATE"
            )

## names(shapes1) <- levels(thesetypes)

names(shapes1) <- levels(thesetypes)
names(cols1) <- levels(thesetypes)
names(vNames) <- levels(thesetypes)

pdf(file = paste(p2figs, "runtimes_base.pdf", sep = ""),
    width = 10,
    height = 6
    )

par(mfrow = c(1, 2))

c <- "House"

for (c in c("House", "Senate")) {

    dfAll2c <- subset(dfAll2a, clab == c)
    dfLabsc <- subset(dfAll2a, sess == 102 & clab == c)
    t <- 1
    dfAll2ct <- subset(dfAll2c, type2 == levels(thesetypes)[t])
    dfLabsct <- subset(dfLabsc, type2 == levels(thesetypes)[t])
    x <- as.character(dfLabsct$type)
    print(x)
    y <- switch(x,
                "ideal" = "IDEAL",
                "wnominate" = "W-NOMINATE",
                "oc" = "OC",
                "fastestLOW" = "EM",
                "fastest" = "EM (high-precision)",
                "fastestMP" = "EM (parallel)",
                "fastestBS" = "EM with Bootstrap"
                )
    plot(x = dfAll2ct$sess,
         y = log(dfAll2ct$time),
         type = "b",
         axes = FALSE,
         ylab = "Time",
         xlab = "Session of Congress",
         xlim = c(102, 112),
         ylim = c(-4, log(60 * 60 * 10)),
         main = c,
         col = cols1[t],
         pch = shapes1[t]
         )
    if (dfLabsct$clab == "House") {
        text(x = 107,
             y = log(dfLabsct$time) -.5,
             labels = y,
             adj = c(.5, 0),
             cex = .65,
             font = 4
             )
    }
    if (dfLabsct$clab == "Senate") {
        text(x = 107,
             y = log(dfLabsct$time) - .5,
             labels = y,
             adj = c(.5, 0),
             cex = .65,
             font = 4
             )
    }
    abline(h = log(c(1, 5, 60, 60 * 60)), col = alpha("black", .15))
    for (t in 2:length(levels(thesetypes))) {
        dfAll2ct <- subset(dfAll2c, type2 == levels(thesetypes)[t])
        dfLabsct <- subset(dfLabsc, type2 == levels(thesetypes)[t])
        x <- as.character(dfLabsct$type)
        print(x)
        print(t)
        y <- switch(x,
                    "ideal" = "IDEAL",
                    "wnominate" = "W-NOMINATE",
                    "oc" = "OC",
                    "fastestLOW" = "EM",
                    "fastest" = "EM (high-precision)",
                    "fastestMP" = "EM (parallel high-precision)",
                    "fastestBS" = "EM with Bootstrap"
                    )
        tm <- log(dfLabsct$time) + .7
        xc <- 102
        if (dfLabsct$clab == "House" & dfLabsct$type == "wnominate") {
            tm0 <- tm + 0
        }
        if (dfLabsct$clab == "House") {
            tmlab <- tm + 0
            if (dfLabsct$type == "wnominate") {
                tmlab <- tm + .1
                xc <- 103
            }
            if (dfLabsct$type == "fastestLOW") {
                tmlab <- tm - .7 * 2
            }
        }
        if (dfLabsct$clab == "Senate") {
            tmlab <- tm + 0
            if (dfLabsct$type == "fastestLOW") {
                tmlab <- tm - .7 * 2
            }
            if (dfLabsct$type %in% c("wnominate")) {
                tmlab <- tm + .7
                xc <- 106
                arrows(x0 = 106,
                       x1 = 106,
                       y0 = tmlab,
                       y1 = tmlab - .6,
                       angle = 15,
                       length = .1,
                       col = alpha("black", .75)
                       )
            }
            if (dfLabsct$type %in% c("oc")) {
                tmlab <- tm + 0
                arrows(x0 = 102,
                       x1 = 102,
                       y0 = tmlab,
                       y1 = tmlab - .6,
                       angle = 15,
                       length = .1,
                       col = alpha("black", .75)
                       )
            }
        }
        text(x = xc,
             y = tmlab,
             font = ifelse(x %in% c("ideal", "fastestBS"), 4, 1),
             labels = y,
             adj = c(0, 0),
             cex = .65
             )
        lines(x = dfAll2ct$sess,
              y = log(dfAll2ct$time),
              type = "b",
              col = cols1[t],
              pch = shapes1[t]
              )
    }
    axis(1,
         at = 102:112,
         cex.axis = 1.2
         )
    axis(2,
         at = log(c(1, 5, 60, 60 * 60)),
         labels = c("1s", "5s", "1m", "1h"),
         cex.axis = 1.3
         )
    box()
}

dev.off()

################################################################################
